// pages/users/wish/form/index.js
import {checkContent,message} from '../../../../utils/util'
const {
  bbsAdd
} = require('../../../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navHeight: getApp().globalData.navHeight,
    navTop: getApp().globalData.navTop,
    menuHeight: getApp().globalData.menuHeight,
    url: ['https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/users/wish_form_button_active.png',
      'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/users/wish_form_button.png',
      'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/users/wish_bg.png',
      'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/users/wish_form_bg.png',
      'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/users/wish_form_dialog.png'
    ],
    number: 0,
    content: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      userInfo: wx.getStorageSync('userInfo')
    })
    setTimeout(() => {
      const pages = getCurrentPages()
      const prevpage = pages[pages.length - 2]
      prevpage.setData({
        isTap: false
      })
    }, 500);

  },
  inputChange(e) {
    const value = e.detail.value
    this.setData({
      number: value.length,
      content: value
    })
  },
  back() {
    var pages = getCurrentPages()
    if (pages.length > 1) {
      wx.navigateBack()
    } else {
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
  },
  async callback(){
    const {content}=this.data
    if(!content){
      wx.showToast({
        title: '请输入内容',
        icon:'none'
      })
      return
    }
     checkContent(content).then(async (res) => {
        const {result}= await  bbsAdd({content})
        wx.showToast({
          title: '发布成功',
          icon:'none'
        })
      setTimeout(res=>{
        const pages = getCurrentPages()
        const prevpage = pages[pages.length - 2]
          wx.navigateBack({
            success: res => {
              prevpage.setData({
                pageNo: 1,
                list: [],
                loading:true
              })
              prevpage.loadData();
            }
          })
      },1000)
      })
      
  },
  submit(e) {
    var temIds = ['OL-JNHv99hu7yKtaf3X1wC9HfmUcIN83r6Xul8UGXjw']
    let self = this;
    message(false, temIds, '', self.callback.bind(self))
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '德道升学',
      imageUrl:'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/users/shareImg.jpg'
    }
  }
})